package com.szholly.plug.activity.model;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.springframework.stereotype.Component;

import com.szholly.utils.util.DateUtil;
import com.szholly.data.general.GlobalDataType;
import com.szholly.data.general.ITable;
import com.szholly.data.general.RowBase;
import com.szholly.data.general.interceptor.DbSelectInterceptorClass;
import com.szholly.data.general.interceptor.InterceptorRegister;
import com.szholly.plug.safe.core.WorkDayCache;
import com.szholly.plug.safe.entity.workday.WorkDayHelper;
import com.szholly.plug.safe.entity.workday.WorkDayUtils;
import com.szholly.plug.safe.entity.workday.WorkTimeEntity;

/**
 * 流程配置拦截器，用于清除流程缓存
 * 1.主配置表保存时，清除所有缓存
 * 2.任务节点保存时，重新计算用户配置信息
 * 3.用户保存时，清除任务节点配置的用户配置信息(包括缓存信息和数据库信息)
 * 4.任务配置使用时，实时调用验证
 */
@Component
public class WorklistQueryInterceptor extends DbSelectInterceptorClass {
	/**
	 * 注册拦截接口
	 */
	static {
		WorklistQueryInterceptor interceptor = new WorklistQueryInterceptor();
		InterceptorRegister.RegDbInterceptor(interceptor);
	}
	
	public WorklistQueryInterceptor(){
		this.regTableName(BpmWorklistTable.TABLE_NAME);
	}

	@Override
	public String getInterceptorName() {
		return "流程待办查询过滤器";
	}


	@Override
	public void BeforeDbSelectOption(ITable table, Object queryFilter) {

	}


	@Override
	public void DbSelectOptionException(ITable table, Object queryFilter,
			Exception ex) {
		
	}

	@SuppressWarnings("unchecked")
	@Override
	public void AfterDbSelectOption(ITable table, Object queryFilter, Object queryData) {
		if(!(queryData instanceof List<?>)){
			return;
		}
		int currentMonth = WorkDayUtils.getMonth(new Date());
		List<RowBase> rows = (List<RowBase>)queryData;
		if (rows != null && rows.size() > 0) {
			// 处理等待时间
			for (RowBase row : rows) {
				Date beginTime = (Date) row.getValue(GlobalDataType.DateTime,
						BpmWorklistRow.FIELD_DBegintime);
				String overtime = (String) row.getValue(GlobalDataType.String,
						BpmWorklistRow.FIELD_REMINDERS_QX);
				HashMap<String, RowBase> holidayMap = WorkDayHelper.getHolidaysCache();
				HashMap<String, String> workTimeMap = WorkDayHelper.getWorkTimesCache();
				if(holidayMap !=null || workTimeMap !=null){
					WorkDayCache.init();
				}
				if(holidayMap !=null && holidayMap.size()>0 && workTimeMap !=null && holidayMap.size()>0){
					String amStart =null;
					String amEnd = null;
					String pmStart = null;
					String pmEnd = null;
					int startMonth = Integer.valueOf(workTimeMap.get(WorkTimeEntity.FIELD_S_MON_START));
					int endMonth = Integer.valueOf(workTimeMap.get(WorkTimeEntity.FIELD_S_MON_END));
					if(currentMonth >=startMonth && currentMonth <= endMonth){
						amStart =workTimeMap.get(WorkTimeEntity.FIELD_S_AM_START);
						amEnd = workTimeMap.get(WorkTimeEntity.FIELD_S_AM_END);
						pmStart = workTimeMap.get(WorkTimeEntity.FIELD_S_PM_START);
						pmEnd = workTimeMap.get(WorkTimeEntity.FIELD_S_PM_END);
					}else{
						amStart =workTimeMap.get(WorkTimeEntity.FIELD_W_AM_START);
						amEnd = workTimeMap.get(WorkTimeEntity.FIELD_W_AM_END);
						pmStart = workTimeMap.get(WorkTimeEntity.FIELD_W_PM_START);
						pmEnd = workTimeMap.get(WorkTimeEntity.FIELD_W_PM_END);
					}
					long totalMinutes =WorkDayUtils.getMinutes(beginTime, new Date(), holidayMap,
							amStart, amEnd, pmStart, pmEnd);
					row.setValue("wait_title", WorkDayUtils.minutesToHours(totalMinutes));
					row.setValue("wait_time", totalMinutes);
					row.setValue("over_time", overtime);
				}else{
					Long day = DateUtil.getDiffDayEx(beginTime);
					row.setValue("wait_time", day);
					if (day > 0) {
						row.setValue("wait_title", day+"天");
					} else {
						day = DateUtil.getDiffDayEx2(beginTime);
						if (day > 0) {
							row.setValue("wait_title", day+"小时");
						} else {
							day = DateUtil.getDiffDayEx3(beginTime);
							if (day > 0) {
								row.setValue("wait_title", day+"分钟");
							} else {
								day = DateUtil.getDiffDayEx4(beginTime);
								if (day >= 0) {
									row.setValue("wait_title", day+"秒");
								}
							}
						}
					}
					row.setValue("over_time", overtime);
				}
			}
		}
	}
}